<?php

namespace App\Models;

class Helps extends Base
{
    protected $table = 'helps';

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--info">锁定</span>',
    ];

    protected $fillable = [
        'id','cate_pid','title','content','type','status','created_at','updated_at'
    ];
    public function cid(){//一级分类
        return $this->belongsTo('App\Models\HelpCategorys', 'cate_pid', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['cate_pid']) && $params['cate_pid']) {
                    $query ->where('cate_pid',$params['cate_pid']);
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
            })->where('status','<>',-1)
            ->with(['cid' => function ($query) {
                $query->select(['id','name'])->get();
            }])
            ->orderBy('created_at', 'desc')->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = '', $limit = 10){
        $params['status'] = 1;
        if(isset($page)) {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['kwd']) && $params['kwd']) {
                        $query ->where('title','like','%' . $params['kwd'] . '%');
                    }
                    if(isset($params['cate_pid']) && $params['cate_pid']) {
                        $query ->where('cate_pid',$params['cate_pid']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                })->where('status','<>',-1)
                ->with(['cid' => function ($query) {
                    $query->select(['id','name'])->get();
                }])
                ->orderBy('created_at', 'desc')->paginate($limit, null, null, $page);
        }else {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['kwd']) && $params['kwd']) {
                        $query ->where('title','like','%' . $params['kwd'] . '%');
                    }
                    if(isset($params['cate_pid']) && $params['cate_pid']) {
                        $query ->where('cate_pid',$params['cate_pid']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                })->where('status','<>',-1)
                ->with(['cid' => function ($query) {
                    $query->select(['id','name'])->get();
                }])
                ->orderBy('created_at','desc')->limit($limit)->get();
        }
        return $lists;
    }

    /**
     * 详情
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function details($params, $fields = ['*']){
        return $this->select($fields)->where($params)->where('status','<>',-1)
            ->with(['cid' => function ($query) {
                $query->select(['id','name'])->get();
            }])->first();
    }

}
